package com.hewanglan.wx.controller;

import com.hewanglan.wx.handler.WxChatMsgFactory;
import com.hewanglan.wx.handler.WxChatMsgHandler;
import com.hewanglan.wx.utils.MessageUtil;
import com.hewanglan.wx.utils.SHA1;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;

@RestController
@Slf4j
public class CallBackController {

    private static final String token="ajwajw123";

    @Resource
    private WxChatMsgFactory wxChatMsgFactory;

    @GetMapping("/callback")
    public String callback(@RequestParam("signature") String signature,
                           @RequestParam("timestamp") String timestamp,
                           @RequestParam("nonce") String nonce,
                           @RequestParam("echostr") String echostr) {
        log.info("get验签请求参数：signature:{}，timestamp:{}，nonce:{}，echostr:{}",
                signature, timestamp, nonce, echostr);
        String shaStr = SHA1.getSHA1(token, timestamp, nonce, "");
        if (signature.equals(shaStr)) {
            return echostr;
        }
        return "unknown";
    }

    @PostMapping(value = "/callback",produces = "application/xml;charset=UTF-8")
    public String callback(@RequestBody String requestBody,
                           @RequestParam("signature") String signature,
                           @RequestParam("timestamp") String timestamp,
                           @RequestParam("nonce") String nonce,
                           @RequestParam(value = "msg_signature",required = false) String msgSignature) {
        log.info("接收到微信消息：requestBody:{},signature:{}，timestamp:{}，nonce:{}",
                requestBody,signature, timestamp, nonce);
        Map<String, String> msgMap = MessageUtil.parseXml(requestBody);
        //工厂加策略设计模式处理用户的不同事件
        String msgType=msgMap.get("MsgType");
        String event=msgMap.get("Event")==null?"":msgMap.get("Event");
        //String msgTypeKey=msgType+"."+event;
        String msgTypeKey= (event.isBlank()||event.isEmpty())?(msgType+event):(msgType+"."+event);
        WxChatMsgHandler wxChatMsgHandler=wxChatMsgFactory.getHandlerByMsgType(msgTypeKey);
        if(Objects.isNull(wxChatMsgHandler))
        {
            return "unknown";
        }
        String replyContent = wxChatMsgHandler.dealMsg(msgMap);

//        String res_data="你好，鹤望兰！";
        return replyContent;
    }

//    <xml><ToUserName><![CDATA[gh_4d2c730a0234]]></ToUserName>\n" +
//            "<FromUserName><![CDATA[owF9J6qmz0ROH-Hng31iSQBGtFfk]]></FromUserName>\n" +
//            "<CreateTime>1711788779</CreateTime>\n" +
//            "<MsgType><![CDATA[event]]></MsgType>\n" +
//            "<Event><![CDATA[subscribe]]></Event>\n" +
//            "<EventKey><![CDATA[]]></EventKey>\n" +
//            "</xml>


//String msg="<xml>\n" +
//        "  <ToUserName><![CDATA["+fromUserName+"]]></ToUserName>\n" +
//        "  <FromUserName><![CDATA["+toUserName+"]]></FromUserName>\n" +
//        "  <CreateTime>12345678</CreateTime>\n" +
//        "  <MsgType><![CDATA[text]]></MsgType>\n" +
//        "  <Content><![CDATA["+res_data+"]]></Content>\n" +
//        "</xml>";


//    <xml><ToUserName><![CDATA[gh_4d2c730a0234]]></ToUserName>
//<FromUserName><![CDATA[owF9J6qmz0ROH-Hng31iSQBGtFfk]]></FromUserName>
//<CreateTime>1711788691</CreateTime>
//<MsgType><![CDATA[text]]></MsgType>
//<Content><![CDATA[这是用户发送的消息]]></Content>
//<MsgId>24506921952693738</MsgId>
//</xml>

//<xml><ToUserName><![CDATA[gh_4d2c730a0234]]></ToUserName>
//<FromUserName><![CDATA[owF9J6qmz0ROH-Hng31iSQBGtFfk]]></FromUserName>
//<CreateTime>1711788742</CreateTime>
//<MsgType><![CDATA[event]]></MsgType>
//<Event><![CDATA[unsubscribe]]></Event>
//<EventKey><![CDATA[]]></EventKey>
//</xml>


}
